// import './public-path';
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'
import {
  renderWithQiankun,
  qiankunWindow,
  QiankunProps,
} from 'vite-plugin-qiankun/dist/helper'
let root: ReactDOM.Root | null = null

function render(props: QiankunProps) {
  const { container } = props
  const root = ReactDOM.createRoot(
    (container
      ? container.querySelector('#root')
      : document.getElementById('root')) as HTMLElement
  )
  root.render(
    <React.StrictMode>
      <App />
    </React.StrictMode>
  )
  return root
}

const initQianKun = () => {
  renderWithQiankun({
    mount(props) {
      root = render(props)
    },
    bootstrap() {
      console.log('bootstrap')
    },
    unmount(props) {
      root?.unmount()
    },
    update(props) {
    },
  })
}

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  root = render({})
} else {
  initQianKun();
}
